<?php

class m_public extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    // get last inserted id
    function get_last_inserted_id() {
        return $this->db->insert_id();
    }

    // get total kelas open
    function get_total_kelas_open($params) {
        $sql = "SELECT COUNT(*) AS 'total' 
                FROM kelas a 
                JOIN kelas_detail b ON a.kelas_kd = b.kelas_kd 
                JOIN mata_kuliah c ON a.makul_kd = c.makul_kd 
                JOIN users d ON a.dosen_kd = d.user_id 
                JOIN jurusan e ON a.jurusan_kd = e.jurusan_kd 
                WHERE b.kelas_st = 'open' AND a.kelas_nm LIKE ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return 0;
        }
    }

    // get data kelas open
    function get_data_kelas_open($params) {
        $sql = "SELECT a.*, b.kelas_detail_kd, b.kelas_tgl, b.kelas_minggu, c.makul_nm, 
                d.nama_lengkap, d.dosen_gelar, e.jurusan_nm, e.jurusan_alias 
                FROM kelas a 
                JOIN kelas_detail b ON a.kelas_kd = b.kelas_kd 
                JOIN mata_kuliah c ON a.makul_kd = c.makul_kd 
                JOIN users d ON a.dosen_kd = d.user_id 
                JOIN jurusan e ON a.jurusan_kd = e.jurusan_kd 
                WHERE b.kelas_st = 'open' AND a.kelas_nm LIKE ? 
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // get detail data kelas
    function get_detail_data_kelas($params) {
        $sql = "SELECT a.*, b.kelas_detail_kd, b.kelas_tgl, b.kelas_minggu, c.makul_nm, 
                b.kelas_st, d.nama_lengkap, d.dosen_gelar, e.jurusan_nm, e.jurusan_alias, 
                f.kompetensi_dasar  
                FROM kelas a 
                JOIN kelas_detail b ON a.kelas_kd = b.kelas_kd 
                JOIN mata_kuliah c ON a.makul_kd = c.makul_kd 
                JOIN users d ON a.dosen_kd = d.user_id 
                JOIN jurusan e ON a.jurusan_kd = e.jurusan_kd 
                JOIN silabus_detail f ON b.silabus_detail_kd = f.silabus_detail_kd 
                WHERE b.kelas_detail_kd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // get instrumen penilaian
    function get_instrumen_penilaian() {
        $sql = "SELECT * FROM instrumen_penilaian ORDER BY instrumen_order ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert info penilaian
    function insert_info_penilaian($params) {
        $sql = "INSERT INTO penilaian_dosen (tahun, semester, kelas_detail_kd, nomor_nim, dosen_kd, mdd) 
            VALUES(?, ?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $params);
    }

    // insert data penilaian
    function insert_data_penilaian($params) {
        $sql = "INSERT INTO penilaian_dosen_detail (input_kd, penilaian_kd, penilaian_value) 
            VALUES(?, ?, ?)";
        return $this->db->query($sql, $params);
    }

    // delete data penilaian
    function delete_data_penilaian($params) {
        $sql = "DELETE FROM penilaian_dosen_detail WHERE input_kd = ?";
        return $this->db->query($sql, $params);
    }

}